﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzDesk.Utility.Web.Controllers;
using EzDesk.Core.Model;
using EzDesk.Clients.DesktopWeb.ViewModel;
using MvcApplication3.Controllers;
using System.Drawing;
using System.IO;

namespace EzDesk.Clients.DesktopWeb.Controllers
{
    public class CoreController : EzDeskAppController
    {
        #region [private methods]

        /// <summary>
        /// 获取图片类型
        /// </summary>
        /// <returns></returns>
        private static ImageFormat getImageType(string imageTypeStr)
        {
            if (imageTypeStr == "JPEG")
            {
                return ImageFormat.Jpeg;
            }
            if (imageTypeStr == "PNG")
            {
                return ImageFormat.Png;
            }

            //todo:
            return null;
        }

        /// <summary>
        /// 获取用户头像的位图数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private Bitmap getHeadIconBitmap(ImageData imageData)
        {
            // 如果服务端没有数据，则返回默认头像
            if (imageData == null)
            {
                var defaultHeadIconPath = Server.MapPath("~/Content/Icons/Gravatar.jpg");
                return new Bitmap(defaultHeadIconPath);
            }

            byte[] headIconDataBytes = imageData.ImageDatas;
            using (var ms = new MemoryStream(headIconDataBytes))
            {
                return new Bitmap(ms);
            }
        }

        private static List<SimpleUserModel> convertToSimpleUserList(List<SimpleUser> usersList)
        {
            var result = new List<SimpleUserModel>();
            usersList.ForEach(p => result.Add(new SimpleUserModel(p)));
            return result;
        }

        #endregion


        public ActionResult CurrentUserLable(bool isDisplayDept)
        {
            var currentUser = EzApiClient.GetSimpleUserById(CurrentUserId);
            ViewData["User"] = currentUser;
            ViewData["IsDisplayDept"] = isDisplayDept;
            return View();
        }

        [HttpGet]
        [Authorize]
        public JsonResult GetAllUserInfo()
        {
            return Json(
                EzApiClient.GetSimpleUserInfos("everyone"),
                JsonRequestBehavior.AllowGet
                );
        }


        /// <summary>
        /// 取得一个部门中的所有用户列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public JsonResult GetUserInfoInDept(Guid deptId)
        {
            Dept dept = EzApiClient.GetDeptById(deptId);
            string groupId = dept.GroupId.ToString();
            var usersList = EzApiClient.GetSimpleUserInfos("|" + groupId);

            List<SimpleUserModel> simpleUserList = convertToSimpleUserList((List<SimpleUser>)usersList);
            return Json(
                simpleUserList,
                JsonRequestBehavior.AllowGet
                );
        }

        [HttpGet]
        public ImageResult HeadIcon(Guid? userId)
        {
            // 默认使用当前用户ID
            if (!userId.HasValue)
            {
                userId = CurrentUserId;
            }
            var imageData = EzApiClient.GetHeadIcon(userId.Value);
            var iconType = imageData == null ? ImageFormat.Jpeg : getImageType(imageData.ImageFormat);
            var headIconBitmap = getHeadIconBitmap(imageData);
            
            return new ImageResult { ImageFormat = iconType, Image = headIconBitmap };
        }


        [HttpGet]
        public JsonResult NavigationList(string pageName)
        {
            // todo: 从后台拿数据
            // todo: 实现让每个页面不一样
            var navList = new List<dynamic>
                           {
                               new {title = "消息", description= "查看消息", funcurl= "/message/inbox"  },
                               new {title = "考勤", description= "考勤", funcurl= "/attendance/PersonalAttendanceInfoList"  },
                               new {title = "假期", description= "假期", funcurl= "/attendance/PersonalLeaveInfoList"  },
                               new {title = "通讯录", description= "公司通讯录", funcurl= "/staff/ContactBook"  },
                           };
            return Json(navList);
        }

        public ActionResult CurrentOrgLable()
        {
            return View();
        }
    }

}
